package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import com.xxxx.server.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 学海无涯
 * @since 2021-11-15
 */
@RestController
@RequestMapping("/system/admin")
@Api(tags = "操作员管理")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation("获取所有操作员")
    @GetMapping("")
    public RespBean<List<Admin>> getAllAdmins(String keywords){
        List<Admin> adminList = adminService.getAllAdmins(keywords);
        return new RespBean<List<Admin>>().success("ok",adminList);
    }

    @ApiOperation("更新操作员")
    @PutMapping("")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if (adminService.updateById(admin)){
            return new RespBean().success("更新成功！");
        }
        return new RespBean().error("更新失败！");
    }

    @ApiOperation("删除操作员")
    @DeleteMapping("")
    public RespBean delAdmin(Integer adminId){
        if (adminService.removeById(adminId)){
            return new RespBean().success("删除成功！");
        }
        return new RespBean().error("删除失败！");
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/roles")
    public RespBean<List<Role>> getAllRoles(){
        List<Role> roles = roleService.list();
        return new RespBean<List<Role>>().success("ok",roles);
    }

    @ApiOperation("更新操作员角色")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminID,Integer[] rIDs){
        return adminService.updateAdminRole(adminID,rIDs);
    }
}
